//
//  myTaskCollectionTableVC.m
//  dataGathering
//
//  Created by apple on 14-11-5.
//  Copyright (c) 2014年 shuili. All rights reserved.
//

#import "myTaskCollectionTableVC.h"
#import "mainVC.h"
#import "WorksetUploadManager.h"

@interface myTaskCollectionTableVC ()

@end

@implementation myTaskCollectionTableVC

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    currentWorkSetManager *manager = [currentWorkSetManager sharedInstance];
    
    //_workSetArray = [[NSMutableArray alloc] initWithCapacity:10];
    _workSetArray = [[NSMutableArray alloc] initWithArray:[manager getWorksetArray]];
    
    NSMutableArray *tempArray = [[NSMutableArray alloc] initWithCapacity:10];
    for(workSet *tempWorkset in _workSetArray){
        if([tempWorkset isDeleted]){
            //[_workSetArray removeObject:tempWorkset];
            [tempArray addObject:tempWorkset];
        }
    }
    for(workSet *www in tempArray)
    {
        [_workSetArray removeObject:www];
    }
    
    //for(workSet *tempWorkset in [manager getWorksetArray]){
    //    if(![tempWorkset isDeleted]){
    //        [_workSetArray addObject:tempWorkset];
   //     }
    //}

    [manager setCurrentWorkSet:[[_workSetArray objectAtIndex:0] name]];
    NSLog(@"workset count %lu", (unsigned long)[_workSetArray count]);
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshTaskCollectionTableView) name:@"refreshTaskCollectionTableView" object:nil];
    
    [[WorksetUploadManager sharedInstance] beginScheduleSync];
    
    
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

-(void)refreshTaskCollectionTableView
{
    currentWorkSetManager *manager = [currentWorkSetManager sharedInstance];
    
    [_workSetArray removeAllObjects];
    _workSetArray = [[NSMutableArray alloc] initWithArray:[manager getWorksetArray]];
    NSMutableArray *tempArray = [[NSMutableArray alloc] initWithCapacity:10];
    
    for(workSet *tempWorkset in _workSetArray){
        if([tempWorkset isDeleted]){
            //[_workSetArray removeObject:tempWorkset];
            [tempArray addObject:tempWorkset];
        }
    }
    for(workSet *www in tempArray)
    {
        [_workSetArray removeObject:www];
    }
    [manager setCurrentWorkSet:[[_workSetArray objectAtIndex:0] name]];
    [self.tableView reloadData];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"updateSelectedWorkSet" object:self];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Table view data source

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 64;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_workSetArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 640, 64)];
    cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    [cell setSelectedBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"u1123_selected.png"]]];
    
    
    UIColor *nameTextColor = [UIColor colorWithRed:77.0/256.0 green:77.0/256.0 blue:77.0/256.0 alpha:1];
    UIColor *creatorTextColor = [UIColor colorWithRed:151.0/256.0 green:151.0/256.0 blue:150.0/256.0 alpha:1];
    
    NSUInteger row = [indexPath row];
    if(row == 0){
        UIImageView *view;
        
        workSet *testObj = [_workSetArray objectAtIndex:row];
        
        if([testObj isCompleted])
        {
            view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pic_我的工作集1_u1146_selected.png"]];
        } else {
            view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pic_我的工作集1_u1529.png"]];
        }
        
        view.frame = CGRectMake(28, 11, 40 , 40);
        view.contentMode = UIViewContentModeScaleAspectFit;
        UILabel *namelabel = [[UILabel alloc] initWithFrame:CGRectMake(96, 21, 300 , 22)];
        [namelabel setText:[testObj name]];
        [namelabel setTextColor:nameTextColor];
        [namelabel setFont:[UIFont systemFontOfSize:16]];
        
        UILabel *creatorlabel = [[UILabel alloc] initWithFrame:CGRectMake(400, 21, 152 , 22)];
        [creatorlabel setText:[testObj creator]];
        [creatorlabel setTextColor:creatorTextColor];
        [creatorlabel setFont:[UIFont systemFontOfSize:16]];
        
        [cell addSubview:view];
        [cell addSubview:namelabel];
        [cell addSubview:creatorlabel];
    } else {
        
        UIImageView *view;
        
        
        workSet *testObj = [_workSetArray objectAtIndex:row];
        
        if([testObj isCompleted])
        {
            view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pic_我的工作集1_u1146_selected.png"]];
        } else {
            view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pic_我的工作集1_u1529.png"]];
        }
        view.frame = CGRectMake(28, 11, 40 , 40);
        view.contentMode = UIViewContentModeScaleAspectFit;
        UILabel *namelabel = [[UILabel alloc] initWithFrame:CGRectMake(96, 21, 300 , 22)];
        [namelabel setText:[testObj name]];
        [namelabel setTextColor:nameTextColor];
        [namelabel setFont:[UIFont systemFontOfSize:16]];
        
        UILabel *creatorlabel = [[UILabel alloc] initWithFrame:CGRectMake(400, 21, 152 , 22)];
        [creatorlabel setText:[testObj creator]];
        [creatorlabel setTextColor:creatorTextColor];
        [creatorlabel setFont:[UIFont systemFontOfSize:16]];
        
        [cell addSubview:view];
        [cell addSubview:namelabel];
        [cell addSubview:creatorlabel];
        
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = [indexPath row];
    currentWorkSetManager *manager = [currentWorkSetManager sharedInstance];
    [manager setCurrentWorkSet:[[_workSetArray objectAtIndex:row] name]];
    
    if(((mainVC*)(self.parentViewController.parentViewController)).state == MainVCStateNewCollection){
        [((mainVC*)(self.parentViewController.parentViewController)) removeNewTaskCollectionView];
    }
    
    
    if(row == 0){
        ((mainVC*)(self.parentViewController.parentViewController)).state = MainVCStateMyCollection;
    } else {
        ((mainVC*)(self.parentViewController.parentViewController)).state = MainVCStateCloudCollection;
    }
    [((mainVC*)(self.parentViewController.parentViewController)) refreshRightSubView];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"updateSelectedWorkSet" object:self];

    
}

-(void)openSelectedRow :(NSInteger)row
{
    [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0] animated:YES scrollPosition:UITableViewScrollPositionMiddle];
    
    currentWorkSetManager *manager = [currentWorkSetManager sharedInstance];
    [manager setCurrentWorkSet:[[_workSetArray objectAtIndex:row] name]];
    
    if(((mainVC*)(self.parentViewController.parentViewController)).state == MainVCStateNewCollection){
        [((mainVC*)(self.parentViewController.parentViewController)) removeNewTaskCollectionView];
    }
    
    
    if(row == 0){
        ((mainVC*)(self.parentViewController.parentViewController)).state = MainVCStateMyCollection;
    } else {
        ((mainVC*)(self.parentViewController.parentViewController)).state = MainVCStateCloudCollection;
    }
    [((mainVC*)(self.parentViewController.parentViewController)) refreshRightSubView];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"updateSelectedWorkSet" object:self];

}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
